package com.corpdata.app.contract.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.corpdata.app.contract.dao.ContractStationMapper;
import com.corpdata.app.contract.entity.ContractStation;
import com.corpdata.app.contract.service.ContractStationService;
import com.corpdata.app.project.entity.ProjectStation;
import com.corpdata.common.result.Result;
import com.corpdata.core.base.AbstractBaseService;

/**
 * 合同分区关联
 * 
 * @author wu
 * @email 64088839@qq.com
 * @date 2018-07-18 16:44:22
 */
@Service
@Transactional
public class ContractStationServiceImpl extends AbstractBaseService<ContractStation> implements ContractStationService {
	@Autowired
	private ContractStationMapper stationMapper;
	

	@Override
	public Result insert(String fkPartition, String contractId, String stationIds) {
		//先删除合同分区下的关联关系
		stationMapper.deleteByPartition(fkPartition,contractId);
		Result result = null;
		String[] stations = stationIds.split(",");
		for(int i = 0;i < stations.length;i++){
			ProjectStation ps = new ProjectStation();
			ps.setId(stations[i]);
			ContractStation record = new ContractStation();
			record.setFkContract(contractId);
			record.setFkPartition(fkPartition);
			record.setStation(ps);
			record.setFlag(0);
			result = super.save(record);
		}
		return result;
	}
	
	

	@Override
	public int modifyFlag(String contractId, String partitionId) {
		Map<String,String> params = new HashMap<String,String>();
		params.put("contractId", contractId);
		params.put("partitionId", partitionId);
		return stationMapper.modifyFlag(params);
	}



	@Override
	public int deleteByPartition(String partitionId, String contractId) {
		// TODO Auto-generated method stub
		return stationMapper.deleteByPartition(partitionId,contractId);
	}



	@Override
	public Result insertList(List<ContractStation> csList) {
		// TODO Auto-generated method stub
		return super.save(csList);
	}



	@Override
	public List<String> selectByContract(String id) {
		// TODO Auto-generated method stub
		return stationMapper.selectByContract(id);
	}



	@Override
	public List<ContractStation> selectAllByContract(String contractId) {
		Map<String, Object> params = new HashMap<String,Object>();
		params.put("contractId", contractId);
		return stationMapper.selectAll(params );
	}


	//删除临时数据
	@Override
	public void delTemporaryData() {
		stationMapper.delTemporaryData();
		
	}
	
}
